package main

import (
	"Ljohn/syncTest/mutex"
	"Ljohn/syncTest/pool"
	"Ljohn/syncTest/smap"
	"fmt"
	"strconv"
	"sync"
	"time"
)

func main() {

	//testGoRun()
	//mutex.InitMutex()
	//muxValueMethod()
	//muxMoreObjTest()
	//PassMutex()
	poolTest()
	//smapTest()
}

func smapTest() {
	//featureTest()
	concurrentTest()
}

func featureTest() {
	m := smap.NewConcurrentMap(1, "string")

	fmt.Println(m.Store(1, "test"))
	fmt.Println(m.Load(1))
	fmt.Println(m.Store("1", "ss"))
	fmt.Println(m.Load(2))
	fmt.Println(m.Load("1"))
}

func concurrentTest() {
	m := smap.NewConcurrentMap(1, "string")
	i := 0
	for i < 10 {
		go func() {
			fmt.Println(m.Store(i, "tt"+strconv.Itoa(i)))
		}()
		i++
	}
	time.Sleep(time.Second * 2)
	i = 0
	for i < 11 {
		fmt.Println(m.Load(i))
		i++
	}
}

func PassMutex() {
	//mutex.ValuePassMutex()
	StructPassMutex()
	fmt.Println("方法执行完成，协程执行中")
	time.Sleep(time.Second * 3)
}

func StructPassMutex() {
	t := mutex.Test{}
	t.Mux.Lock()
	defer t.Mux.Unlock()
	fmt.Printf("外部锁地址：%p\n", &t.Mux)
	//此时的锁会是被锁状态的时刻的一个复制
	go mutex.StructValuePassMutex(t)
	t1 := mutex.Test1{Mux: &sync.Mutex{}}
	t1.Mux.Lock()
	defer t1.Mux.Unlock()
	fmt.Printf("外部锁地址：%p\n", &t1.Mux)
	//此时的锁会针对指针判断是否一个锁对象。
	go mutex.StructPtrPassMutex(t1)
	time.Sleep(time.Second * 1)
}

func muxValueMethod() {
	o := mutex.GetMuxObj()
	go o.Write("Test")
	go func() {
		//如果锁生效应该是等待的。
		time.Sleep(time.Second * 1)
		o.Read()
	}()
	time.Sleep(time.Second * 5)
}

//值类型返回时，锁不是一个对象
func muxMoreObjTest() {
	o1 := mutex.GetMuxObj()
	o2 := mutex.GetMuxObj()
	go o1.Write("Test")
	go func() {
		time.Sleep(time.Second * 1)
		o2.Read()
	}()
	time.Sleep(time.Second * 5)
}

func poolTest() {
	pool.OutLine("Li", 11)
	pool.OutLine("Go", 21)
}

func testGoRun() {
	i := 0
	for {
		i++
		if i > 2 {
			break
		}
		go mutex.MutexTest()
	}
	for {

	}
}
